package com.think.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.think.teaching.model.ExamTest;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.Dictionary;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
public interface ExamTestMapper extends BaseMapper<ExamTest> {
	@Select("SELECT * FROM exam_test WHERE paper_id "
			+ "IN(SELECT id FROM exam_paper WHERE course_id=#{courseId} AND delflag=0 AND state=1) AND delflag=0 "
			+ "AND state=1 AND exam_test.end_time >  NOW() - INTERVAL 3 DAY ORDER BY exam_test.start_time DESC")
	List<ExamTest> getExamTestValidListByCourseId(Integer courseId);

	@Select("SELECT * FROM exam_test WHERE id=#{id} AND delflag=0")
	@Result(column = "paper_id",property = "zones",
	one = @One(select = "com.think.teaching.mapper.PaperZoneMapper.getPaperZoneDetails",
			fetchType = FetchType.EAGER))
	Map<String,Object> getExamPaperWithQuestions(Integer id);

	@Select("<script>SELECT t.*,course.course_name,course_id,exam_paper.title as paper_name,paper_id FROM exam_test t,course,exam_paper WHERE t.end_time&lt;CURRENT_TIMESTAMP AND t.delflag=0 AND t.state=1 AND t.paper_id=exam_paper.id AND exam_paper.course_id =course.id "
			+ "<if test='course gt 0'>  AND course_id =#{course}</if>"
			+ " ORDER BY t.end_time DESC  "
			+ "</script>")
	IPage<Map<String, Object>> ListFinishedExamTest(IPage<Map<String, Object>> page, @Param("course") Integer course);

	@Select("<script> SELECT exam_test.*,exam_paper.title FROM exam_test LEFT JOIN exam_paper ON exam_test.paper_id=exam_paper.id WHERE exam_test.delflag=0"
			+
			"<if test='state gt 0'> AND exam_test.state =#{state}</if>"
			+
			"<if test='id gt 0'> AND exam_test.paper_id IN (SELECT exam_paper.id FROM exam_paper WHERE exam_paper.course_id=#{id})</if>"
			+
			"<if test='type gt 0'> AND exam_test.paper_id IN (SELECT exam_paper.id FROM exam_paper WHERE exam_paper.type=#{type})</if>"
			+
			"<if test='tag gt 0'> AND exam_test.paper_id IN (SELECT paper_tag.paper_id FROM paper_tag WHERE paper_tag.tag=#{tag})</if>"
			+
			"</script>")
	IPage<Map<String, Object>> ListExamTest(IPage<Map<String, Object>> page,
											@Param("state") Integer state,
											@Param("type") Integer type,
											@Param("tag") Integer tag,
											@Param("id") Integer id);
}
